import { TagView, TagsViewState } from '@/stores/interface'
import { pinia } from '../index'
export const useTagsViewStore = defineStore('eco-tagsView', {
  state: (): TagsViewState => ({
    visitedViews: [],
    cachedViews: [],
    selectedTag: undefined
  }),
  getters: {
    getVisitedViews: state => state.visitedViews,
    getCachedViews: state => state.cachedViews,
    getSelectedTag: state => state.selectedTag
  },
  actions: {
    addView(view: TagView): void {
      this.addVisitedView(view)
      this.addCachedView(view)
    },
    /**
     * 新增tag
     *
     * @param view TagView
     */
    addVisitedView(view: TagView): void {
      if (this.visitedViews.some(v => v.path === view.path)) return
      if (view?.noTagsView) return
      if (view.affix) {
        this.visitedViews.unshift(view)
      } else {
        this.visitedViews.push(
          Object.assign({}, view, {
            title: view?.title || 'no-name'
          })
        )
      }
    },
    /**
     * 新增缓存
     */
    addCachedView(view: TagView): void {
      const viewName = view.name
      if (this.cachedViews.includes(viewName)) return
      if (view.keepAlive) this.cachedViews.push(viewName)
    },
    /**
     * 删除所有缓存和tag
     */
    delAllViews() {
      this.delAllVisitedViews()
      this.delAllCachedViews()
    },

    delLeftViews(view: TagView) {
      const currIndex = this.visitedViews.findIndex(v => v.path === view.path)
      if (currIndex === -1) {
        return
      }
      this.visitedViews = this.visitedViews.filter((v, i) => {
        return v?.affix || v.path === view.path || i > currIndex
      })
      this.delCachedView(view)
    },
    // 删除右侧
    delRightViews(view: TagView) {
      const currIndex = this.visitedViews.findIndex(v => v.path === view.path)
      if (currIndex === -1) {
        return
      }
      this.visitedViews = this.visitedViews.filter((v, i) => {
        return v?.affix || v.path === view.path || i < currIndex
      })
      this.delCachedView(view)
    },
    // 删除其它
    delOthersViews(view: TagView) {
      this.delOthersVisitedViews(view)
      this.delCachedView(view)
    },
    // 删除其它tag
    delOthersVisitedViews(view: TagView) {
      this.visitedViews = this.visitedViews.filter(v => {
        return v?.affix || v.path === view.path
      })
    },

    // 删除某个
    delView(view: TagView) {
      this.delVisitedView(view)
      this.delCachedView(view)
    },

    // 删除tag
    delVisitedView(view: TagView) {
      for (const [i, v] of this.visitedViews.entries()) {
        if (v.path === view.path) {
          this.visitedViews.splice(i, 1)
          break
        }
      }
    },
    // 删除缓存
    delCachedView(view: TagView) {
      const viewName = view.name
      const index = this.cachedViews.indexOf(viewName)
      index > -1 && this.cachedViews.splice(index, 1)
    },

    /**
     * 删除所有tag
     * @returns 删除所有tag
     */
    delAllVisitedViews() {
      const affixTags = this.visitedViews.filter(tag => tag?.affix)
      this.visitedViews = affixTags
    },
    // 删除所有缓存
    delAllCachedViews() {
      this.cachedViews = []
    },
    updateVisitedView(view: TagView) {
      for (let v of this.visitedViews) {
        if (v.path === view.path) {
          v = Object.assign(v, view)
          break
        }
      }
    },
    // 设置当前选中的tag
    setSelectedTag(tag: TagView) {
      this.selectedTag = tag
    },
    setTitle(title: string, path?: string) {
      for (const v of this.visitedViews) {
        if (v.path === (path ?? this.selectedTag?.path)) {
          v.title = title
          break
        }
      }
    }
  }
})

export const useTagsViewStoreWithOut = () => {
  return useTagsViewStore(pinia)
}
